package com.ruoyi.media.channelsMail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.channelsMail.domain.bo.MediaChannelsMailBo;
import com.ruoyi.media.channelsMail.domain.vo.MediaChannelsMailVo;
import com.ruoyi.media.channelsMail.domain.MediaChannelsMail;
import com.ruoyi.media.channelsMail.mapper.MediaChannelsMailMapper;
import com.ruoyi.media.channelsMail.service.IMediaChannelsMailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频号小店开通Service业务层处理
 *
 * @author dqw
 * @date 2023-04-01
 */
@RequiredArgsConstructor
@Service
public class MediaChannelsMailServiceImpl implements IMediaChannelsMailService {

    private final MediaChannelsMailMapper baseMapper;

    /**
     * 查询视频号小店开通
     */
    @Override
    public MediaChannelsMailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询视频号小店开通列表
     */
    @Override
    public TableDataInfo<MediaChannelsMailVo> queryPageList(MediaChannelsMailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaChannelsMail> lqw = buildQueryWrapper(bo);
        Page<MediaChannelsMailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询视频号小店开通列表
     */
    @Override
    public List<MediaChannelsMailVo> queryList(MediaChannelsMailBo bo) {
        LambdaQueryWrapper<MediaChannelsMail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaChannelsMail> buildQueryWrapper(MediaChannelsMailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaChannelsMail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaChannelsMail::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaChannelsMail::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getCookies()), MediaChannelsMail::getCookies, bo.getCookies());
        lqw.eq(StringUtils.isNotBlank(bo.getSpuId()), MediaChannelsMail::getSpuId, bo.getSpuId());
        return lqw;
    }

    /**
     * 新增视频号小店开通
     */
    @Override
    public Boolean insertByBo(MediaChannelsMailBo bo) {
        MediaChannelsMail add = BeanUtil.toBean(bo, MediaChannelsMail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改视频号小店开通
     */
    @Override
    public Boolean updateByBo(MediaChannelsMailBo bo) {
        MediaChannelsMail update = BeanUtil.toBean(bo, MediaChannelsMail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaChannelsMail entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除视频号小店开通
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
